/**
 * @author 黄波
 * 创建日期：2011Oct 13, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.os.util.Mail;
import com.platform.pojo.UserInfo;
import com.platform.user.bo.UserBo;

/**
 * 找回密码
 */
public class RetrievePasswdAction extends NJCCSuport {

	private String loginName;
	private String email;
	private String verifyCode;
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if(loginName == null || loginName.equals("")) {
			out.flush();
			out.write("<script>alert('请填写帐号！');history.back();</script>");
			out.close();
			return null;
		}
		if(email == null || email.equals("")) {
			out.flush();
			out.write("<script>alert('请填写该帐号注册邮箱！');history.back();</script>");
			out.close();
			return null;
		}
		final UserInfo user = UserBo.getInstance().getUser1(loginName);
		if(user == null) {
			out.flush();
			out.write("<script>alert('没有该用户！');history.back();</script>");
			out.close();
			return null;
		}
		if(!user.getEmail().equals(email.trim())) {
			out.flush();
			out.write("<script>alert('邮箱地址错误！');history.back();</script>");
			out.close();
			return null;
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证验证码是否填写正确
		if(!session.getAttribute("rand").toString().toLowerCase().equals(verifyCode.trim().toLowerCase())) {
			out.flush();
			out.write("<script>alert('验证码填写错误！');history.back();</script>");
			out.close();
			return null;
		}
		String systemProfile = "/system.properties";
		InputStream stream = new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("/") + systemProfile));
		final Properties properties = new Properties();
		properties.load(stream);
		stream.close();
		//验证成功，发送邮箱给注册邮箱
		new Thread() {
			public void run() {
				Mail.PRO_FILE = properties;
				Mail.sendMail("密码找回成功-南京化工职业技术学院资源共享平台！", "您的密码为：" + user.getLoginPasswd() + "，<a href='" + properties.getProperty("server_static_addr") + "' style='color:red;'>点击这里，进行访问</a>！<br/><br/>---------------<br/>南京化工职业技术学院--资源共享平台<br/>", user.getEmail());

			}
		}.start();
		out.flush();
		out.write("<script>alert('密码找回邮件已发送！');history.back();</script>");
		out.close();
		return null;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
}
